package com.automation.ljf.util;

import com.automation.ljf.domain.dataobject.CaseDO;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.util.concurrent.TimeUnit;

/**
 * 自动化测试操作基本工具类
 *
 * @author lijiefeng
 * @since 2021/8/24
 */
@Slf4j
public class DriverUtils {

    /**
     * 打开网页并设置参数
     * @param caseDO    测试用例VO
     * @return  ChromeDriver
     */
    public static ChromeDriver setUrlPage(CaseDO caseDO){
        setChromePath();
        ChromeOptions options = new ChromeOptions();
        //options.addArguments("--headless"); //无头模式，不弹出浏览器，后台运行
        options.addArguments("--no-sandbox");
        options.addArguments("--disable-blink-features=AutomationControlled");
        options.addArguments("--disable-gpu");
        options.addArguments("--disable-dev-shm-usage");
        options.addArguments("--disable-infobars");
        options.setExperimentalOption("excludeSwitches", new String[]{"enable-automation"});
        ChromeDriver driver = new ChromeDriver(options);
        timeoutWait(driver, null);
        driver.get(caseDO.getCaseUrl());
        DriverUtils.maxWindows(driver);

        return driver;
    }

    /**
     * 设置谷歌浏览器插件位置
     */
    public static void setChromePath(){
        System.setProperty("webdriver.chrome.driver",
                "C:\\driver\\chromedriver.exe");
    }


    /**
     * 设置加载超时等待(隐式) 默认5秒
     * @param driver    driver
     * @param time     时间
     */
    public static void timeoutWait(ChromeDriver driver, Integer time){
        if (time == null){
            driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        }else {
            driver.manage().timeouts().implicitlyWait(time, TimeUnit.SECONDS);
        }
    }

    /**
     * 判断元素是否存在
     * @param driver     driver
     * @param by        页面元素
     * @return  boolean
     */
    public static boolean isElementExist(ChromeDriver driver, By by) {
        try {
            driver.findElement(by);
            timeoutWait(driver, null);
            return true;
        } catch (Exception e) {
            log.error("不存在此元素：" + by);
            return false;
        }
    }

    /**
     *  滚动条下拉页面
     * @param driver    driver
     * @param value     下拉值
     */
    public static void dropDownPage(ChromeDriver driver, Integer value){
        //先点击一下页面
        WebElement body = driver.findElement(By.cssSelector("body"));
        body.click();
        if (value == -1){
            body.sendKeys(Keys.PAGE_DOWN);
        }else{
            //移动到指定的坐标(相对当前的坐标移动)
            ((JavascriptExecutor) driver).executeScript("window.scrollBy(0, "+ value +")");
        }
    }

    /**
     * 刷新页面
     * @param driver    driver
     */
    public static void refresh(ChromeDriver driver){
        driver.navigate().refresh();

    }

    /**
     * 最大化浏览器
     * @param driver    driver
     */
    public static void maxWindows(ChromeDriver driver){
        driver.manage().window().maximize();
    }



}
